package com.currency.mall.ui.my;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.currency.mall.Constant;
import com.currency.mall.R;
import com.currency.mall.adapter.BankCardAdapter;
import com.currency.mall.entity.BankCardBean;
import com.currency.mall.netWork.NetWork;
import com.mifengs.commonbase.base.BaseActivity;
import com.mifengs.commonbase.util.StatusBarCompat;
import com.mifengs.commonbase.util.StatusBarUtils;
import com.mifengs.commonbase.util.ToastUitl;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by Administrator on 2018/12/24.
 */

public class BankCardActivity extends BaseActivity {

    @BindView(R.id.tv_common_address)
    RecyclerView tvCommonAddress;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.ll_not_data)
    RelativeLayout llNotData;
    private BankCardAdapter mBankCardAdapter;
    private Dialog authDialog;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bank_card);
        ButterKnife.bind(this);
        StatusBarCompat.setStatusBarColor(this, ContextCompat.getColor(getContext(), R.color.title_color));
        StatusBarUtils.StatusBarLightMode(this);
        init();
    }

    private void init() {
        tvCommonAddress.setLayoutManager(new LinearLayoutManager(getContext()));
        tvCommonAddress.setNestedScrollingEnabled(false);
        mBankCardAdapter = new BankCardAdapter(null, getContext());
        refreshLayout.setEnableLoadMore(false);
        tvCommonAddress.setAdapter(mBankCardAdapter);

        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                if (refreshLayout != null) {
                    getBankCardList();
                }
            }
        });
        getBankCardList();
    }


    public void finishRefresh() {
        if (refreshLayout != null)
            refreshLayout.finishRefresh();
    }

    public void getBankCardList() {
        NetWork.getUserApi().getBankCardData()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<BankCardBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        finishRefresh();
                    }

                    @Override
                    public void onNext(BankCardBean bankCardBean) {
                        finishRefresh();
                        if (bankCardBean.getCode().equals(Constant.SUCCESS_CODE)) {
                            if(bankCardBean.getList().size() == 0){
                                llNotData.setVisibility(View.VISIBLE);
                            }else{
                                llNotData.setVisibility(View.GONE);
                            }
                            mBankCardAdapter.setNewData(bankCardBean.getList());
                        } else {
                            ToastUitl.show(bankCardBean.getMsg(), 200);
                        }
                    }
                });
    }

    private void authDialogShow() {
        authDialog = new Dialog(getContext(), R.style.dialog);
        authDialog.setContentView(R.layout.dialog_not_title);
        authDialog.show();
        authDialog.setCancelable(false);
        TextView tvInfo = (TextView) authDialog.findViewById(R.id.tv_info);
        tvInfo.setText("未完成实名认证");
        Button okBtn = authDialog.findViewById(R.id.bt_ok);
        okBtn.setText("去认证");
        okBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                authDialog.dismiss();
                toAuthActivity();
            }
        });
        Button cancleBtn = authDialog.findViewById(R.id.bt_cancle);
        cancleBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                authDialog.dismiss();
            }
        });
    }

    private  void toAuthActivity(){
        startActivity(new Intent(getContext(),NameAutoActivity.class));
    }


    @OnClick({R.id.ll_back, R.id.btn_add_bank_card})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_back:
                finish();
                break;
            case R.id.btn_add_bank_card:
                startActivityForResult(new Intent(getContext(), AddBankCardActivity.class), 1000);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == 1001) {
            getBankCardList();
        }
    }
}
